from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index, Boolean, DateTime
from sqlalchemy.orm import sessionmaker, relationship
from passlib.apps import custom_app_context as pwd_context
import datetime
import uuid
from .database import Base


def gen_id():
    return uuid.uuid1().hex


# 创建用户表
class Users(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    # 唯一标识符
    uuid = Column(String(32), default=gen_id, index=True, unique=True)
    # 用户名
    username = Column(String(32), index=True)
    # 加密后的密码
    password_hash = Column(String(120))
    # 性别
    gender = Column(String(10))
    # 头像
    head_sculpture = Column(String(100))
    # 昵称
    nickname = Column(String(32))
    # 全名
    full_name = Column(String(64))
    # 生日
    birthday = Column(String(64))
    # 邮箱
    email = Column(String(120))
    # 个人简介
    profile = Column(String(1000))
    # 密钥
    key = Column(String(64), default=None)
    is_active = Column(Boolean(), default=1)
    # 职业
    occupation = Column(String(50))
    # 公司
    company = Column(String(50))
    # 公司地址
    company_address = Column(String(50))
    # 技术标签
    tech_label = Column(String(100))
    # 兴趣标签
    hobby_tag = Column(String(100))
    # 联系地址
    contact_address = Column(String(100))
    # 手机号
    phone = Column(String(20))
    # 备注
    remark = Column(String(32))
    # 最后登录时间
    last_login_time = Column(DateTime)
    # 创建时间
    create_time = Column(DateTime, default=datetime.datetime.now)
    # 更新时间
    update_time = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
    __table_args__ = (
        # UniqueConstraint('username', name='uix_id_name'),
        #  Index('ix_id_name', 'username'),
    )



class UserPermission(Base):
    __tablename__ = 'userpermission'
    id = Column(Integer, primary_key=True)
    uuid = Column(String(32), default=gen_id, index=True, unique=True)
    user_uuid = Column(String(32), index=True)
    username = Column(String(32), index=True)
    scopesname = Column(String(32))
    scopesgroup = Column(String(32))
    path = Column(String(128))
    description = Column(String(128))
    is_active = Column(Boolean(), default=True)
    create_time = Column(DateTime, default=datetime.datetime.now)
    update_time = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)


# 用户API申请权限
class ApiKeyList(Base):
    __tablename__ = 'user_api_key'
    id = Column(Integer, primary_key=True)
    apply_key = Column(String(32), default=gen_id, index=True, unique=True)
    username = Column(String(32), index=True)
    apply_name = Column(String(32))
    apply_id = Column(String(20))
    path = Column(String(128))
    residue_degree = Column(Integer,default=0)
    apply_image = Column(String(100))
    description = Column(String(128))
    is_active = Column(Boolean(), default=True)
    create_time = Column(DateTime, default=datetime.datetime.now)
    update_time = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)


# 创建用户账单表
class UsersCharge(Base):
    __tablename__ = 'user_charge'
    id = Column(Integer, primary_key=True)
    user_uuid = Column(String(32), index=True)
    username = Column(String(32), index=True)
    aid = Column(String(32))
    api = Column(String(64))
    # date = Column(DateTime, default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    result = Column(Integer)
    remark = Column(String(200))
    create_time = Column(DateTime, default=datetime.datetime.now)
    update_time = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)


class APISource(Base):
    __tablename__ = 'api_source'
    id = Column(Integer, primary_key=True)
    aid = Column(String(32), index=True)
    apply_name = Column(String(64), index=True)
    cate_name = Column(String(32))
    cid = Column(String(20))
    description = Column(String(500))
    # 用来区别是公司还是个人用户, 0代表个人用户，1代表公司用户
    user_check = Column(Boolean)
    # 提示，比如"本接口仅支持企业用户，企业认证后才能调用"
    limit_remark = Column(String(64))
    api_image = Column(String(20))
    # 预留字段，暂无意义
    is_active = Column(Boolean, default='1')

class Company(Base):
    __tablename__ = 'company_info'
    id = Column(Integer, primary_key=True)
    bid = Column(String(32))
    company_name = Column(String(200))
    city = Column(String(200))
    country = Column(String(200))
    country_iso_code = Column(String(200),default='1234')
    company_local_name = Column(String(200),default='local name')